<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-11
 * Time: 11:57
 */

namespace app\src\order\logic;


use app\src\base\helper\ExceptionHelper;
use app\src\base\logic\BaseLogic;
use app\src\order\model\OrdersItem;
use think\Db;
use think\exception\DbException;

class OrdersItemLogic extends BaseLogic
{
    public function _init()
    {
        $this->setModel(new OrdersItem());
    }

    public function queryAll($ordersCodes)
    {
        try{

            if(is_array($ordersCodes)){
                $ordersCodes = implode(",",$ordersCodes);
            }

            $map = [
                'item.order_code'=>['in',$ordersCodes ]
            ];

            $result = Db::table("itboye_orders_item")->alias("item")
                ->field("dt.name as unit_name,p.product_code,item.name,item.img,item.price,item.ori_price,item.sku_id,item.psku_id,item.sku_desc,item.count,item.order_code,item.createtime,item.dt_goods_unit,item.dt_origin_country,item.id,item.weight")
                ->join(["itboye_product"=>"p"],"p.id = item.p_id","left")
                ->join(["common_datatree"=>"dt"],"dt.parentid = 37 and dt.code = item.dt_goods_unit","left")
                ->where($map)
                ->select();
            $list = [];
            foreach ($result  as $item){
                $item['unit_name'] = lang('unit_'.$item['dt_goods_unit']);
                array_push($list,$item);
            }
            return $this->apiReturnSuc($list);
        }catch (DbException $ex){
            return $this->apiReturnErr(ExceptionHelper::getErrorString($ex));
        }
    }
}